package net.gazhi.delonix.core.web;

import java.text.SimpleDateFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.validation.AbstractBindingResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DefaultMessageCodesResolver;
import org.springframework.validation.DefaultMessageCodesResolver.Format;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import net.gazhi.delonix.core.beans.CustomArrayEditor;
import net.gazhi.delonix.core.beans.CustomSqlDateEditor;
import net.gazhi.delonix.core.beans.CustomSqlTimeEditor;
import net.gazhi.delonix.core.beans.CustomSqlTimestampEditor;

/**
 * 基于注解的 Controller 基类
 * 
 * @author Jeffrey Lin
 * 
 */
public class AbstractAnnotationController {

	private final static String SIMPLE_RESULT_VIEW = "global/result";
	private final static String SIMPLE_MSG_VIEW = "global/msg";

	protected Log logger = LogFactory.getLog(this.getClass());

	/**
	 * 返回简单的结果视图
	 * 
	 * @param result
	 * @return
	 */
	protected static final ModelAndView resultView(BindingResult result) {
		return new ModelAndView(SIMPLE_RESULT_VIEW).addObject("result", result);
	}

	/**
	 * 返回简单的消息结果视图
	 * 
	 * @param msg
	 * @return
	 */
	protected static final ModelAndView msgView(String msg) {
		return new ModelAndView(SIMPLE_MSG_VIEW).addObject("msg", msg);
	}

	/**
	 * 初始化请求数据邦定器，实现数据类型自动转换
	 * 
	 * @param binder
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		doInitBinder(binder);
	}

	/**
	 * 初始化表单版定器
	 */
	private static void doInitBinder(WebDataBinder binder) {

		// 错误消息的 message code 格式改为后置的
		AbstractBindingResult result = (AbstractBindingResult) binder.getBindingResult();
		DefaultMessageCodesResolver messageCodesResolver = (DefaultMessageCodesResolver) result.getMessageCodesResolver();
		messageCodesResolver.setMessageCodeFormatter(Format.POSTFIX_ERROR_CODE);

		// 清除字符串前后的空格
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));

		// 设置绑定日期类型的格式
		binder.registerCustomEditor(java.sql.Date.class, new CustomSqlDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
		// 设置绑定时间类型的格式
		binder.registerCustomEditor(java.sql.Time.class, new CustomSqlTimeEditor(new SimpleDateFormat("HH:mm:ss"), true));
		// 设置绑定 Timestamp 的格式
		binder.registerCustomEditor(java.sql.Timestamp.class, new CustomSqlTimestampEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));

		// 自定义的数组转换器
		binder.registerCustomEditor(String[].class, new CustomArrayEditor(String.class, binder.getConversionService()));
		binder.registerCustomEditor(Integer[].class, new CustomArrayEditor(Integer.class, binder.getConversionService()));
		binder.registerCustomEditor(Long[].class, new CustomArrayEditor(Long.class, binder.getConversionService()));
		binder.registerCustomEditor(Float[].class, new CustomArrayEditor(Float.class, binder.getConversionService()));
		binder.registerCustomEditor(Double[].class, new CustomArrayEditor(Double.class, binder.getConversionService()));

	}

}
